package com.csmk.springboot_simple.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {


    @Value("${resource.base-dir}")
    private String resourceBaseDir;

    /**
     * 允许跨域请求
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //放行哪些原始域
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("*");
    }

//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        configurer.addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class));
//    }
    /**
     * 该方法等同于配置文件中的
     *   resources:
     *     static-locations: file:${resource.base-dir}
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 本地文件上传路径
        registry.addResourceHandler("/proFile/**")
                .addResourceLocations("file:" + resourceBaseDir + "/");
    }

    /**
     * 自定义controller方法参数注入
     * 自动注入userId
     * @param resolvers
     */
//    @Override
//    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
//        resolvers.add(new HandlerMethodArgumentResolver() {
//            @Override
//            public boolean supportsParameter(MethodParameter methodParameter) {
//                // 找到controller方法中参数名为userId的形参
//                return Objects.equals(methodParameter.getParameterName(), "userId");
//            }
//
//            @Override
//            public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
//                // 从session中获取userId返回至supportsParameter判断为true的形参中
//                return nativeWebRequest.getAttribute("userId", NativeWebRequest.SCOPE_SESSION);
//            }
//
//        });
//    }
    /**
     * 自定义返回处理器
     */
//    @Override
//    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
//        handlers.add(new HandlerMethodReturnValueHandler() {
//            @Override
//            public boolean supportsReturnType(MethodParameter methodParameter) {
//                return false;
//            }
//
//            @Override
//            public void handleReturnValue(Object o, MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest) throws Exception {
//
//            }
//        });
//    }
}
